package fts.ftsui;

import java.io.File;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.ProgressBar;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Table;
import org.mortbay.jetty.Connector;
import org.mortbay.jetty.Server;
import org.mortbay.jetty.bio.SocketConnector;
import org.mortbay.jetty.servlet.Context;
import org.mortbay.jetty.servlet.ServletHandler;
import org.mortbay.jetty.servlet.ServletHolder;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;

import com.cloudgarden.resource.SWTResourceManager;

import fts.ftsmonitoring.ftsTaggers.IAutoTagger;
import fts.ftscontrol.Facade;
import fts.ftsdata.domain.ITag;
import fts.ftsdata.domain.ITaggedFile;
import fts.ftsdata.domain.IWatchFolder;
import fts.ftsdata.domain.IWatchWildcard;
import fts.ftsui.observers.FileWatcherLabelObserver;
import fts.ftsui.observers.InitializerObserver;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class NewSWTApp extends org.eclipse.swt.widgets.Composite {
	Facade facade = null;
	String currentExpression = ""; 
	Server server = null;
	
	private TabFolder tabFolder1;
	private TabItem tabInitializaer;
	private Combo cmbTagFileTagSelection;
	private Label lblAutoTaggersDir;
	private Button btnAutoTaggersRefresh;
	private Button btnRefreshTags;
	private Label lblAutoTaggers;
	private Table tableAutoTaggers;
	private Button btnAddWatchWildcard;
	private Text txtWildcard;
	private Label lblConfigWatchWildcards;
	private List listWatchWildcards;
	private List listWatchFolders;
	private Button btnDeleteWatchFolder;
	private Button btnAddWatchFolder;
	private Label lblConfigWatchFolders;
	private Composite composite5;
	private Button btnFind;
	private List listFindFilesResult;
	private Button btnResetTagExpression;
	private Button btnSetTagExpression;
	private Combo cmbFindFilesTagSelection;
	private Button cbTagNot;
	private Button rbTagOr;
	private Button rbTagAnd;
	private Text txtTagExpresion;
	private Composite composite4;
	private Button btnTagFIle;
	private Label lblTagFileTagSelection;
	private Label lblSelectedFile;
	private Button btnBrowse;
	private Composite composite3;
	private Button btnInitStop;
	private Button btnInitStart;
	private ProgressBar progressInit;
	private Label lblInitStatusText;
	private Label lblInitStatusLabel;
	private TabItem tabConfiguration;
	private Label lblStatusLabel;
	private Composite composite2;
	private Button btnWatcherStop;
	private Button btnWatcherStart;
	private Label lblStatusText;
	private Composite composite1;
	private TabItem tabFindFIles;
	private TabItem tabTagFiles;
	private TabItem tabWatcher;
	private TableColumn tableAutoTaggersFileCol;
	private TableColumn tableAutoTaggersDescCol;

	{
		//Register as a resource user - SWTResourceManager will
		//handle the obtaining and disposing of resources
		SWTResourceManager.registerResourceUser(this);
	}

	public NewSWTApp(Composite parent, int style) throws Exception {
		super(parent, style);
		
		BeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource("config.xml"));
		//IDataFactory dataFactory=(IDataFactory)beanFactory.getBean("dataFactory");
		//IFileMonitoringService monitoringService = (IFileMonitoringService)beanFactory.getBean("monitoringService");
		facade = (Facade)beanFactory.getBean("facade");
		//facade = new Facade(dataFactory, monitoringService);

		initGUI(facade);
		
		facade.addFileObserver(new FileWatcherLabelObserver(lblStatusText));
		facade.addInitializerObserver(new InitializerObserver(lblInitStatusText, progressInit));
		resetAllTagLists();
		resetWatchFoldersList();
		resetAutoTaggersTable();
		
        server = new Server();
        Connector connector=new SocketConnector();
        connector.setPort(8181);
        server.setConnectors(new Connector[]{connector});
        
        Context root = new Context(server,"/",Context.DEFAULT);

        root.addServlet(new ServletHolder(new WatcherServlet(facade)), "/watcher");
        root.addServlet(new ServletHolder(new InitializerServlet(facade)), "/init");


        server.start();

	}

	private void resetAllTagLists()
	{
		resetTagSelection(cmbTagFileTagSelection);
		resetTagSelection(cmbFindFilesTagSelection);
	}
	
	public void dispose()
	{
		facade.stopInitializerThread();
		facade.stopFileMonitoring();
		try
		{
			server.stop();
		}
		catch (Exception e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void resetTagSelection(Combo combo)
	{
		Collection<? extends ITag> tags = new ArrayList<ITag>();
		try
		{
			tags = facade.getAllTags();
		}
		catch (Exception e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Iterator<? extends ITag> tagIter = tags.iterator();
		combo.removeAll();
		while(tagIter.hasNext())
		{
			combo.add(tagIter.next().getName());
		}
	}
	private void resetWatchFoldersList()
	{
		Iterator<? extends IWatchFolder> folderIter = facade.getWatchFoldersIterator();
		listWatchFolders.removeAll();
		while(folderIter.hasNext())
		{
			listWatchFolders.add(folderIter.next().getPath());
		}
		listWatchWildcards.removeAll();
	}	
	
	private void resetWatchWildcardsList()
	{
		listWatchWildcards.removeAll();
		String[] sel = listWatchFolders.getSelection();
		if(sel != null && sel.length == 1)
		{
			Iterator<? extends IWatchFolder> folderIter = facade.getWatchFoldersIterator();
			IWatchFolder folder = null;
			while(folderIter.hasNext())
			{
				IWatchFolder f = folderIter.next();
				if(Paths.get(f.getPath()).equals(Paths.get(sel[0])))
				{
					folder = f;
					break;
				}
			}
			if(folder != null)
			{
				Iterator<? extends IWatchWildcard> iterW = folder.getWildcardsIterator();
				while(iterW.hasNext())
				{
					listWatchWildcards.add(iterW.next().getWildcard());
				}
			}
		}
	}	
	private HashMap<String, IAutoTagger> autoTaggersMap = new HashMap<String, IAutoTagger>();
	@SuppressWarnings({ "deprecation", "unchecked" })
	private void resetAutoTaggersTable()
	{
		tableAutoTaggers.clearAll();
		autoTaggersMap.clear();
		System.getProperty("user.dir");

		String folderStr = System.getProperty("user.dir") + "\\AutoTaggersClasses";
		lblAutoTaggersDir.setText(folderStr);
		
		File folder = new File(folderStr);
		if(!new File(folderStr+"\\fts\\ftsmonitoring\\ftsTaggers").exists())
		{
			return;
		}
		File[] files = new File(folderStr+"\\fts\\ftsmonitoring\\ftsTaggers").listFiles();
		for(File file : files)
		{
			if(file.getName().endsWith(".class"))
			{
				IAutoTagger tagger = null;
				Class<fts.ftsmonitoring.ftsTaggers.IAutoTagger> cls = null;
				try {
				    // Convert File to a URL
				    URL url = folder.toURL();          // file:/c:/myclasses/
				    URL[] urls = new URL[]{url};

				    // Create a new class loader with the directory
				    ClassLoader cl = new URLClassLoader(urls);
				    
				    // Load in the class; MyClass.class should be located in
				    cls = (Class<fts.ftsmonitoring.ftsTaggers.IAutoTagger>)cl.loadClass("fts.ftsmonitoring.ftsTaggers." + file.getName().replaceAll(".class", ""));
				    tagger = cls.newInstance();
				} catch (Exception e) {
					lblAutoTaggersDir.setText(e.toString().replaceAll("fts.ftsmonitoring.", "").replaceAll("java.lang.ClassCast", ""));
				}
				if(tagger != null)
				{
					autoTaggersMap.put(file.getName(), tagger);
					TableItem item1 = new TableItem(tableAutoTaggers,0);
					item1.setText(new String[]{file.getName(),tagger.getDescription()});
				}
			}
		}
	}
	
	/**
	* Initializes the GUI.
	*/
	private void initGUI(Facade facade) {
		try {
			this.setBackground(SWTResourceManager.getColor(192, 192, 192));
			this.setLayout(null);
			{
				tabFolder1 = new TabFolder(this, SWT.NONE);
				{
					tabWatcher = new TabItem(tabFolder1, SWT.NONE);
					tabWatcher.setText("Watcher");
					{
						composite1 = new Composite(tabFolder1, SWT.NONE);
						tabWatcher.setControl(composite1);
						composite1.setLayout(null);
						{
							lblStatusLabel = new Label(composite1, SWT.NONE);
							lblStatusLabel.setText("Status:");
							lblStatusLabel.setBounds(20, 27, 41, 21);
						}
						{
							lblStatusText = new Label(composite1, SWT.NONE);
							lblStatusText.setText("Not Available");
							lblStatusText.setBounds(60, 27, 178, 21);
						}
						{
							btnWatcherStart = new Button(composite1, SWT.PUSH | SWT.CENTER);
							btnWatcherStart.setText("Start");
							btnWatcherStart.setBounds(20, 54, 113, 19);
							btnWatcherStart.addSelectionListener(new SelectionAdapter() {
								public void widgetSelected(SelectionEvent evt) {
									btnWatcherStartWidgetSelected(evt);
								}
							});
						}
						{
							btnWatcherStop = new Button(composite1, SWT.PUSH | SWT.CENTER);
							btnWatcherStop.setText("Stop");
							btnWatcherStop.setBounds(20, 79, 113, 20);
							btnWatcherStop.addSelectionListener(new SelectionAdapter() {
								public void widgetSelected(SelectionEvent evt) {
									btnWatcherStopWidgetSelected(evt);
								}
							});
						}
					}
				}
				{
					tabInitializaer = new TabItem(tabFolder1, SWT.NONE);
					tabInitializaer.setText("Initializer");
					{
						composite2 = new Composite(tabFolder1, SWT.NONE);
						tabInitializaer.setControl(composite2);
						composite2.setLayout(null);
						{
							lblInitStatusLabel = new Label(composite2, SWT.NONE);
							lblInitStatusLabel.setText("Status:");
							lblInitStatusLabel.setBounds(33, 27, 42, 19);
						}
						{
							lblInitStatusText = new Label(composite2, SWT.NONE);
							lblInitStatusText.setText("IDLE");
							lblInitStatusText.setBounds(81, 27, 150, 19);
						}
						{
							progressInit = new ProgressBar(composite2, SWT.NONE);
							progressInit.setBounds(33, 52, 585, 17);
							progressInit.setMinimum(0);
							progressInit.setMaximum(100);
						}
						{
							btnInitStart = new Button(composite2, SWT.PUSH | SWT.CENTER);
							btnInitStart.setText("Start");
							btnInitStart.setBounds(33, 81, 93, 19);
							btnInitStart.addSelectionListener(new SelectionAdapter() {
								public void widgetSelected(SelectionEvent evt) {
									btnInitStartWidgetSelected(evt);
								}
							});
						}
						{
							btnInitStop = new Button(composite2, SWT.PUSH | SWT.CENTER);
							btnInitStop.setText("Stop");
							btnInitStop.setBounds(132, 81, 93, 19);
							btnInitStop.addSelectionListener(new SelectionAdapter() {
								public void widgetSelected(SelectionEvent evt) {
									btnInitStopWidgetSelected(evt);
								}
							});
						}
					}
				}
				{
					tabTagFiles = new TabItem(tabFolder1, SWT.NONE);
					tabTagFiles.setText("Tag Files");
					{
						composite3 = new Composite(tabFolder1, SWT.NONE);
						tabTagFiles.setControl(composite3);
						composite3.setLayout(null);
						{
							btnBrowse = new Button(composite3, SWT.PUSH | SWT.CENTER);
							btnBrowse.setText("Browse...");
							btnBrowse.setBounds(12, 26, 63, 16);
							btnBrowse.addSelectionListener(new SelectionAdapter() {
								public void widgetSelected(SelectionEvent evt) {
									btnBrowseWidgetSelected(evt);
								}
							});
						}
						{
							lblSelectedFile = new Label(composite3, SWT.NONE);
							lblSelectedFile.setBounds(87, 26, 569, 16);
						}
						{
							cmbTagFileTagSelection = new Combo(composite3, SWT.NONE);
							cmbTagFileTagSelection.setBounds(114, 54, 170, 23);
						}
						{
							lblTagFileTagSelection = new Label(composite3, SWT.NONE);
							lblTagFileTagSelection.setText("Select/Create Tag:");
							lblTagFileTagSelection.setBounds(12, 57, 99, 17);
						}
						{
							btnTagFIle = new Button(composite3, SWT.PUSH | SWT.CENTER);
							btnTagFIle.setText("Tag File");
							btnTagFIle.setSize(60, 30);
							btnTagFIle.setBounds(12, 94, 60, 30);
							btnTagFIle.addSelectionListener(new SelectionAdapter() {
								public void widgetSelected(SelectionEvent evt) {
									btnTagFIleWidgetSelected(evt);
								}
							});
						}
						{
							btnRefreshTags = new Button(composite3, SWT.PUSH | SWT.CENTER);
							btnRefreshTags.setText("Refresh Tags");
							btnRefreshTags.setBounds(12, 152, 109, 30);
							btnRefreshTags.addSelectionListener(new SelectionAdapter() {
								public void widgetSelected(SelectionEvent evt) {
									btnRefreshTagsWidgetSelected(evt);
								}
							});
						}
					}
				}
				{
					tabFindFIles = new TabItem(tabFolder1, SWT.NONE);
					tabFindFIles.setText("Find Files");
					{
						composite4 = new Composite(tabFolder1, SWT.NONE);
						tabFindFIles.setControl(composite4);
						composite4.setLayout(null);
						{
							txtTagExpresion = new Text(composite4, SWT.NONE);
							txtTagExpresion.setBounds(12, 51, 644, 23);
							txtTagExpresion.setEditable(false);
						}
						{
							rbTagAnd = new Button(composite4, SWT.RADIO | SWT.LEFT);
							rbTagAnd.setText("AND");
							rbTagAnd.setBounds(12, 19, 54, 23);
							rbTagAnd.setSelection(true);
						}
						{
							rbTagOr = new Button(composite4, SWT.RADIO | SWT.LEFT);
							rbTagOr.setText("OR");
							rbTagOr.setBounds(66, 19, 60, 23);
						}
						{
							cbTagNot = new Button(composite4, SWT.CHECK | SWT.LEFT);
							cbTagNot.setText("NOT");
							cbTagNot.setBounds(138, 19, 60, 23);
						}
						{
							cmbFindFilesTagSelection = new Combo(composite4, SWT.NONE);
							cmbFindFilesTagSelection.setBounds(210, 19, 184, 23);
						}
						{
							btnSetTagExpression = new Button(composite4, SWT.PUSH | SWT.CENTER);
							btnSetTagExpression.setText("Set");
							btnSetTagExpression.setBounds(411, 19, 60, 20);
							btnSetTagExpression.addSelectionListener(new SelectionAdapter() {
								public void widgetSelected(SelectionEvent evt) {
									btnSetTagExpressionWidgetSelected(evt);
								}
							});
						}
						{
							btnResetTagExpression = new Button(composite4, SWT.PUSH | SWT.CENTER);
							btnResetTagExpression.setText("Reset");
							btnResetTagExpression.setBounds(483, 19, 60, 20);
							btnResetTagExpression.addSelectionListener(new SelectionAdapter() {
								public void widgetSelected(SelectionEvent evt) {
									btnResetTagExpressionWidgetSelected(evt);
								}
							});
						}
						{
							listFindFilesResult = new List(composite4, SWT.NONE);
							listFindFilesResult.setBounds(12, 93, 644, 223);
						}
						{
							btnFind = new Button(composite4, SWT.PUSH | SWT.CENTER);
							btnFind.setText("Find");
							btnFind.setBounds(560, 19, 60, 20);
							btnFind.addSelectionListener(new SelectionAdapter() {
								public void widgetSelected(SelectionEvent evt) {
									btnFindWidgetSelected(evt);
								}
							});
						}
					}
				}
				{
					tabConfiguration = new TabItem(tabFolder1, SWT.NONE);
					tabConfiguration.setText("Configuration");
					{
						composite5 = new Composite(tabFolder1, SWT.NONE);
						tabConfiguration.setControl(composite5);
						composite5.setLayout(null);
						composite5.setBackground(SWTResourceManager.getColor(221, 221, 221));
						{
							lblConfigWatchFolders = new Label(composite5, SWT.NONE);
							lblConfigWatchFolders.setText("Watch Folders");
							lblConfigWatchFolders.setBounds(12, 12, 98, 18);
							lblConfigWatchFolders.setBackground(SWTResourceManager.getColor(221, 221, 221));
						}
						{
							btnAddWatchFolder = new Button(composite5, SWT.PUSH | SWT.CENTER);
							btnAddWatchFolder.setText("Add");
							btnAddWatchFolder.setBounds(12, 30, 60, 15);
							btnAddWatchFolder.addSelectionListener(new SelectionAdapter() {
								public void widgetSelected(SelectionEvent evt) {
									btnAddWatchFolderWidgetSelected(evt);
								}
							});
						}
						{
							btnDeleteWatchFolder = new Button(composite5, SWT.PUSH | SWT.CENTER);
							btnDeleteWatchFolder.setText("Delete");
							btnDeleteWatchFolder.setBounds(78, 30, 60, 15);
							btnDeleteWatchFolder.addSelectionListener(new SelectionAdapter() {
								public void widgetSelected(SelectionEvent evt) {
									btnDeleteWatchFolderWidgetSelected(evt);
								}
							});
						}
						{
							listWatchFolders = new List(composite5, SWT.NONE);
							listWatchFolders.setBounds(12, 51, 471, 97);
							listWatchFolders.addSelectionListener(new SelectionAdapter() {
								public void widgetSelected(SelectionEvent evt) {
									listWatchFoldersWidgetSelected(evt);
								}
							});
						}
						{
							listWatchWildcards = new List(composite5, SWT.NONE);
							listWatchWildcards.setBounds(489, 51, 167, 97);
						}
						{
							lblConfigWatchWildcards = new Label(composite5, SWT.NONE);
							lblConfigWatchWildcards.setText("Wildcards");
							lblConfigWatchWildcards.setBounds(489, 12, 60, 18);
							lblConfigWatchWildcards.setBackground(SWTResourceManager.getColor(221, 221, 221));
						}
						{
							txtWildcard = new Text(composite5, SWT.NONE);
							txtWildcard.setBounds(555, 30, 101, 15);
						}
						{
							btnAddWatchWildcard = new Button(composite5, SWT.PUSH | SWT.CENTER);
							btnAddWatchWildcard.setText("Add");
							btnAddWatchWildcard.setBounds(489, 30, 60, 15);
							btnAddWatchWildcard.addSelectionListener(new SelectionAdapter() {
								public void widgetSelected(SelectionEvent evt) {
									btnAddWatchWildcardWidgetSelected(evt);
								}
							});
						}
						{
							tableAutoTaggers = new Table(composite5, SWT.CHECK);
							tableAutoTaggersFileCol  = new TableColumn(tableAutoTaggers,SWT.LEFT);
							tableAutoTaggersFileCol.setText("File");
							tableAutoTaggersFileCol.setWidth(304);
							tableAutoTaggersDescCol  = new TableColumn(tableAutoTaggers,SWT.LEFT);
							tableAutoTaggersDescCol.setText("Description");
							tableAutoTaggersDescCol.setWidth(340);

							tableAutoTaggers.setBounds(12, 193, 644, 123);
							tableAutoTaggers.addSelectionListener(new SelectionAdapter() {
								public void widgetSelected(SelectionEvent evt) {
									tableAutoTaggersWidgetSelected(evt);
								}
							});
						}
						{
							lblAutoTaggers = new Label(composite5, SWT.NONE);
							lblAutoTaggers.setText("Auto Taggers");
							lblAutoTaggers.setBounds(12, 170, 77, 17);
							lblAutoTaggers.setBackground(SWTResourceManager.getColor(221, 221, 221));
						}
						{
							btnAutoTaggersRefresh = new Button(composite5, SWT.PUSH | SWT.CENTER);
							btnAutoTaggersRefresh.setText("Refresh");
							btnAutoTaggersRefresh.setBounds(95, 170, 60, 17);
							btnAutoTaggersRefresh.addSelectionListener(new SelectionAdapter() {
								public void widgetSelected(SelectionEvent evt) {
									btnAutoTaggersRefreshWidgetSelected(evt);
								}
							});
						}
						{
							lblAutoTaggersDir = new Label(composite5, SWT.NONE);
							lblAutoTaggersDir.setBounds(172, 170, 484, 17);
						}
					}
				}
				tabFolder1.setSelection(0);
				tabFolder1.setBounds(0, 0, 676, 356);
			}
			this.layout();
			pack();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	* Auto-generated main method to display this 
	* org.eclipse.swt.widgets.Composite inside a new Shell.
	 * @throws Exception 
	*/
	public static void main(String[] args) throws Exception {
		
		final Display display = Display.getDefault();
		final Shell shell = new Shell(display);
		NewSWTApp inst = new NewSWTApp(shell, SWT.NULL);
		Point size = inst.getSize();
		shell.setLayout(new FillLayout());
		shell.layout();
		if(size.x == 0 && size.y == 0) {
			inst.pack();
			shell.pack();
		} else {
			Rectangle shellBounds = shell.computeTrim(0, 0, size.x, size.y);
			shell.setSize(shellBounds.width, shellBounds.height);
		}
		shell.open();
		
		
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		
		inst.dispose();

	}
	
		
	private void btnWatcherStopWidgetSelected(SelectionEvent evt) {
		facade.stopFileMonitoring();
	}
	
	private void btnWatcherStartWidgetSelected(SelectionEvent evt) {
		facade.startFileMonitoring();
	}
		
	private void btnInitStartWidgetSelected(SelectionEvent evt) {
		facade.startInitializerThread();
	}
	
	private void btnInitStopWidgetSelected(SelectionEvent evt) {
		facade.stopInitializerThread();
	}
	
	private void btnBrowseWidgetSelected(SelectionEvent evt) {
		try
		{
			FileDialog fd = new FileDialog(evt.display.getActiveShell(), SWT.OPEN);
	        fd.setText("Open");
	        //fd.setFilterPath("C:/");
	        String[] filterExt = {"*.*", "*.txt", "*.jpg", ".mp3" };
	        fd.setFilterExtensions(filterExt);
	        String selected = fd.open();
	        lblSelectedFile.setText(selected);
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
		}
	}
	
	private void btnTagFIleWidgetSelected(SelectionEvent evt) {
		String fileName = lblSelectedFile.getText();
		String tagName = cmbTagFileTagSelection.getText();
		if(fileName == null || fileName.length() == 0)
		{
			MessageBox msgBox = new MessageBox(evt.display.getActiveShell());
			msgBox.setMessage("Select file first");
			msgBox.open();
			return;
		}
		if(tagName == null || tagName.length() == 0)
		{
			MessageBox msgBox = new MessageBox(evt.display.getActiveShell());
			msgBox.setMessage("Select tag first");
			msgBox.open();
			return;
		}
		try
		{
			facade.addTagToFile(fileName, tagName);
			MessageBox msgBox = new MessageBox(evt.display.getActiveShell());
			msgBox.setMessage("File tagged");
			msgBox.open();
			lblSelectedFile.setText("");
			cmbTagFileTagSelection.setText("");
			resetAllTagLists();
			return;
		}
		catch(Exception ex)
		{
			MessageBox msgBox = new MessageBox(evt.display.getActiveShell());
			msgBox.setMessage("An error has occured");
			msgBox.open();
		}
	}
	
	private void btnSetTagExpressionWidgetSelected(SelectionEvent evt) {
		String expr = txtTagExpresion.getText();
		String tag = cmbFindFilesTagSelection.getText();
		if(tag == null || (tag = tag.replaceAll("\"", "")).length() == 0)
		{
			MessageBox msgBox = new MessageBox(evt.display.getActiveShell());
			msgBox.setMessage("Select tag first");
			msgBox.open();
			return;
		}
		
		if(txtTagExpresion.getText() == null)
		{
			expr = "";
			currentExpression = "";
		}
		if(expr.length() > 0)
		{
			if(rbTagAnd.getSelection())
			{
				expr += " AND ";
				currentExpression += " & ";
			}
			else
			{
				expr += " OR ";
				currentExpression += " | ";
			}
		}
		if(cbTagNot.getSelection())
		{
			expr += "NOT ";
			currentExpression += "!";
		}
		expr += "\"" + tag + "\"";
		currentExpression += tag;
		txtTagExpresion.setText(expr);
	}
	
	private void btnResetTagExpressionWidgetSelected(SelectionEvent evt) {
		txtTagExpresion.setText("");
		currentExpression = "";
	}
	
	private void btnFindWidgetSelected(SelectionEvent evt) {
		try
		{
			listFindFilesResult.removeAll();
			Collection<? extends ITaggedFile> files = facade.getFilesByTagExpression(currentExpression, 0, 10000);
			Iterator<? extends ITaggedFile> iter = files.iterator();
			while(iter.hasNext())
			{
				listFindFilesResult.add(iter.next().getName());
			}
		}
		catch(Exception ex)
		{
			MessageBox msgBox = new MessageBox(evt.display.getActiveShell());
			msgBox.setMessage(ex.getMessage());
			msgBox.open();
			return;
		}
	}
	
	private void btnAddWatchFolderWidgetSelected(SelectionEvent evt) {
		DirectoryDialog  dlg = new DirectoryDialog(evt.display.getActiveShell());
		String selectedDirectory = dlg.open();
		if(selectedDirectory != null && selectedDirectory.length() > 0)
		{
			try
			{
				facade.addWatchFolder(selectedDirectory);
				resetWatchFoldersList();
			}
			catch (Exception e)
			{
				MessageBox msgBox = new MessageBox(evt.display.getActiveShell());
				msgBox.setMessage("An error has occured");
				msgBox.open();
			}
		}
	}
	
	private void btnDeleteWatchFolderWidgetSelected(SelectionEvent evt) {
		String[] sel = listWatchFolders.getSelection();
		if(sel != null && sel.length > 0)
		{
			for(String folder : sel)
			{
				try
				{
					facade.deleteWatchFolder(folder);
					resetWatchFoldersList();
				}
				catch (Exception e)
				{
					MessageBox msgBox = new MessageBox(evt.display.getActiveShell());
					msgBox.setMessage("An error has occured");
					msgBox.open();
				}
			}
		}
	}
	
	private void listWatchFoldersWidgetSelected(SelectionEvent evt) {
		resetWatchWildcardsList();
	}
	
	private void btnAddWatchWildcardWidgetSelected(SelectionEvent evt) {
		String wc = txtWildcard.getText();
		if(wc == null || wc.length() == 0)
		{
			MessageBox msgBox = new MessageBox(evt.display.getActiveShell());
			msgBox.setMessage("Enter wildcard first");
			msgBox.open();
			return;
		}
		String[] sel = listWatchFolders.getSelection();
		if(sel != null && sel.length > 0)
		{
			for(String folder : sel)
			{
				try
				{
					facade.addWatchFolderWildcard(folder, wc);
					resetWatchWildcardsList();
				}
				catch (Exception e)
				{
					MessageBox msgBox = new MessageBox(evt.display.getActiveShell());
					msgBox.setMessage("An error has occured");
					msgBox.open();
				}
			}
		}
	}
	
	private void tableAutoTaggersWidgetSelected(SelectionEvent evt) {
		System.out.println("tableAutoTaggers.widgetSelected, event="+evt);
		//TODO add your code for tableAutoTaggers.widgetSelected
		TableItem tblItem = (TableItem)evt.item;
		if(tblItem.getChecked())
		{
			facade.addAutoTagger(autoTaggersMap.get(tblItem.getText()));
		}
		else
		{
			facade.removeAutoTagger(autoTaggersMap.get(tblItem.getText()));
		}
	}
	
	private void btnRefreshTagsWidgetSelected(SelectionEvent evt) {
		resetAllTagLists();
	}
	
	private void btnAutoTaggersRefreshWidgetSelected(SelectionEvent evt) {
		resetAutoTaggersTable();
	}

}
